# coding:utf-8
from django.contrib.auth import login, logout, authenticate
from django.http import JsonResponse
from django.shortcuts import redirect
from django.urls import reverse
from django.views.generic import View
from app.libs.base_render import render_to_response
from django.core.paginator import Paginator
from app.model.video import Video
from app.model.auth import ClientUser
from app.utils.permission import dashboard_auth


class Login(View):
    TEMPLATE = 'dashboard/auth/login.html'

    def get(self, request):

        if request.user.is_authenticated:
            return redirect(reverse('dashboard_index'))

        to = request.GET.get('to', '')

        data = {'error': '', 'to': to}
        return render_to_response(request, self.TEMPLATE, data)

    def post(self, request):

        username = request.POST.get('username')
        password = request.POST.get('password')
        to = request.GET.get('to', '')

        data = {}

        if not password or not username:
            data['error'] = '不可为空'
            return render_to_response(request, self.TEMPLATE, data=data)

        # exists = User.objects.filter(username=username).exists()
        # data['error'] = '没有该用户'
        # if not exists:
        #     return render_to_response(request, self.TEMPLATE, data=data)

        user = authenticate(username=username, password=password)

        # if not user.is_active:
        #     data['error'] = '该账号已被封'
        #     return render_to_response(request, self.TEMPLATE, data=data)

        if not user:
            data['error'] = '用户名密码错误'
            return render_to_response(request, self.TEMPLATE, data=data)

        login(request, user)

        if to:
            return redirect(to)

        return redirect(reverse('dashboard_index'))


class Logout(View):

    def get(self, request):
        logout(request)
        return redirect(reverse('login'))


# 显示所有用户
class AdminManager(View):
    TEMPLATE = 'dashboard/auth/admin.html'

    @dashboard_auth
    def get(self, request):
        users = ClientUser.objects.all()
        page = request.GET.get('page', 1)
        # 每页显示两个用户
        p = Paginator(users, 2)
        total_page = p.num_pages

        if int(page) <= 1:
            page = 1
        current_page = p.get_page(int(page)).object_list

        data = {'users': current_page, 'total': total_page, 'page_num': int(page)}
        return render_to_response(request, self.TEMPLATE, data=data)


# 更新用户状态
class UpdateUserStatus(View):

    @dashboard_auth
    def get(self, request):

        status = request.GET.get('status', 'on')
        name = request.GET.get('name', '')

        _status = 1 if status == 'on' else 0
        selected_user = ClientUser.objects.filter(username=name)
        for i in range(len(selected_user)):
            selected_user[i].status = _status
            selected_user[i].save()
            # print(selected_user[i].is_active)
        return redirect(reverse('admin_manager'))


 # 修改电影信息
class ModifyMovie(View):
    TEMPLATE = 'dashboard/auth/change_movie.html'

    def get(self, request):
        return render_to_response(request, self.TEMPLATE)

    def post(self, request):

        title = request.POST.get('title', '')
        new_title = request.POST.get('new_title', '')
        genres = request.POST.getlist('genres_list', '')

        temp = ''
        for i in range(len(genres)):
            temp += genres[i] + '|'
        temp = temp[:-1]

        t = Video.objects.filter(title=title).exists()
        if not t:
            data = {'msg': '没有该电影'}
            return JsonResponse(data)
        Video.objects.filter(title=title).update(title=new_title, genres=temp)

        data = {'msg': '修改成功'}
        return JsonResponse(data)
